/**
 * 
 */
package truerefactor.codesmell;

import java.util.ArrayList;
import java.util.List;

import truerefactor.graph.AttributeNode;
import truerefactor.graph.ClassNode;
import truerefactor.graph.CodeGraph;
import truerefactor.graph.CodeNode;
import truerefactor.refactor.Refactoring;
import truerefactor.refactor.RefactoringFactory;

/**
 * TemporaryField - An implementation of a detection and a refactoring sequence
 * decision algorithms for the Temporary Field Code Smell.
 * 
 * @author Isaac Griffith
 */
public class TemporaryField extends CodeSmell {

    /**
     * Creates a new instance of TemporaryField
     */
    public TemporaryField()
    {
        this.shortName = "TMPF";
        this.name = "Temporary Field";
        this.description = "";
    }

    /*
     * (non-Javadoc)
     * @see
     * truerefactor.codesmell.CodeSmell#measure(truerefactor.graph.CodeGraph,
     * boolean, java.util.List)
     */
    @Override
    protected double measure(CodeGraph graph, boolean initial, List<List<Refactoring>> sequences)
    {
        List<ClassNode> classes = graph.getClasses();
        double smellFound = 0;

        for (ClassNode cNode : classes)
        {
            int ivmc = 0; // the number of methods that reference each instance
                          // variable defined in a class
            List<Refactoring> sequence = new ArrayList<Refactoring>();
            List<CodeNode> attrs = cNode.getAttributes();
            for (CodeNode node : attrs)
            {
                if (node instanceof AttributeNode)
                {
                    AttributeNode aNode = (AttributeNode) node;
                    ivmc += aNode.getAssociatedMethods().size();
                }
            }

            if (ivmc <= 0)
            {
                smellFound++;

                if (initial)
                {
                    CodeNode[] operands = new CodeNode[attrs.size() + 1];
                    operands[0] = cNode;
                    for (int i = 1; i < operands.length; i++)
                    {
                        operands[i] = attrs.get(i - 1);
                    }
                    Refactoring refactoring = RefactoringFactory.getInstance().createExtractClass(operands);
                    sequence.add(refactoring);
                    sequences.add(sequence);
                }
            }
        }

        return smellFound;
    }
}
